import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
type DictState = {
    dictData: Array<{
        keys: string;
        value: any;
    }>;
};
const initialState: DictState = {
    dictData: [],

}
const dictSlice = createSlice({
    name: 'dict',
    initialState,
    reducers: {
        setDict: (state, { payload: { _key, value } }) => {
            if (_key !== null && _key !== "") {
                state.dictData.push({
                    keys: _key,
                    value: value
                });
            }
        },
        removeDict(state, { payload: _key }) {
            try {
                for (let i = 0; i < state.dictData.length; i++) {
                    if (state.dictData[i].keys == _key) {
                        state.dictData.splice(i, 1);
                    }
                }
            } catch (e) {

            }

        }
    },

    // https://zhuanlan.zhihu.com/p/382487951
    // 一般用于处理异步操作，这里是Pending: 当异步操作开始时，Fulfilled: 当异步操作成功完成时，Rejected: 当异步操作遇到错误时
    extraReducers: (builder) => {
        // builder.addCase(getUserInfoSlice.pending, () => {
        //     // console.log('pending 进行中');
        // })
        // builder.addCase(getUserInfoSlice.fulfilled, (state, { payload }) => {
        //     // console.log( '1 fulfilled', { payload, state } )
        // })
        // builder.addCase(getUserInfoSlice.rejected, (state, action) => {
        //     // userSlice.actions.CLEAR_USER_INFO()
        // })
    }


})

export const {  setDict, removeDict } = dictSlice.actions




const dictReducer = dictSlice.reducer
export default dictReducer
